﻿@using NetCoreCMS.Framework.Core.Models 
@using NetCoreCMS.Framework.Utility
@using System.Collections
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    Layout = Constants.AdminLayoutName;
    Title = "Module Management";
    SubTitle = "Modules";
}

@*<link href="/NetCoreCMS.Modules.Cms/css/page.css" rel="stylesheet" />
<script src="/NetCoreCMS.Modules.Cms/js/manageModule.js"></script>*@

<style>
    .ModuleManage li.active a {
        color: #fff !important;
        background-color: #337ab7 !important;
        border-color: #2e6da4 !important;
    }
    .moduleClassBc{

    }
</style>

<div class="ModuleManage">
    <ul class="nav nav-tabs">
        <li class="active">
            <a href="#modules" data-toggle="tab" aria-expanded="true"> Modules</a>
        </li>
        <li class="">
            <a href="#modulesOnline" data-toggle="tab"> Online Modules</a>
        </li>
        <li class="">
            <a href="#coreModules" data-toggle="tab"> Core Modules</a>
        </li>
    </ul>
    <div class="tab-content fullHeight widget-layout-container">
        <div class="tab-pane active" id="modules">
            <div class="">
                <div class="panel panel-default">
                    @*<div class="panel-heading">
                        @SubTitle
                        <div class="pull-right"><a asp-controller="CmsTheme" asp-action="Install" class="btn btn-outline btn-primary btn-xs">Install</a></div>
                    </div>*@
                    <div class="panel-body">
                        @if (TempData.Keys.Contains("ModuleSuccessMessage"))
                        {
                            <div class="alert alert-success alert-dismissable">
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                                @TempData["ModuleSuccessMessage"] - <a href="/Home/RestartHost">Restart Now</a> <a href="#" class="alert-link"></a>.
                            </div>
                        }
                        <table id="pageListDt" class="table table-bordered" cellspacing="0" width="100%">
                            <thead>
                                <tr><th>Name</th><th>Description</th><th>Dependency</th><th>Status</th></tr>
                            </thead>
                            <tbody>
                                @foreach (NccModule item in ViewBag.Modules)
                                {
                                    if (item.Path.Contains("\\Core\\") != true)
                                    {
                                        var bc = "";
                                        var c = "";
                                        if (item.ModuleStatus == NccModule.NccModuleStatus.Active)
                                        {
                                            bc = "#d0d0d0";
                                        }
                                        else if(item.ModuleStatus == NccModule.NccModuleStatus.Installed || item.ModuleStatus == NccModule.NccModuleStatus.Inactive)
                                        {
                                            bc = "#f0f0f0";
                                        }
                                        <tr style="background-color:@bc; color: @c;">
                                            <td>
                                                <strong>@item.ModuleTitle </strong><br style="margin-top:2px; margin-bottom:2px;" />
                                                @Html.Raw(Core.Admin.Lib.ModuleHelper.GetActionText(item))
                                            </td>
                                            <td>
                                                @item.Description
                                                <br />
                                                <i>
                                                    Version: <b>@item.Version</b> | By: <a href="@item.WebSite" target="_blank" style="font-size:12px; color: @c;">@item.Author</a> | Category: @item.Category
                                                </i>
                                            </td>
                                            <td>
                                                @{ bool isFirst = true;}
                                                @foreach (var dep in item.Dependencies)
                                                {
                                                    if (isFirst == false)
                                                    {
                                                        <span>, </span>
                                                    }
                                                    isFirst = false;
                                                    <span>@dep.Name (@dep.VersionNumber)</span>
                                                }
                                            </td>
                                            <td>@item.ModuleStatus</td>
                                        </tr>
                                    }
                                }
                            </tbody>
                        </table>

                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->
            </div>
        </div>
        <div class="tab-pane" id="modulesOnline">
            <div class="">
                <div class="panel panel-default">
                    <div class="panel-body" >
                        @* 
                            Ajax List show

                            Already Installed
                            Update
                            Download
                        *@
                        <table id="moduleGalleryOnline" class="table table-striped table-bordered" cellspacing="0" width="100%">
                            <thead>
                                <tr>
                                    <th>Module</th>
                                    <th>Version</th>
                                    <th>Category</th>
                                    <th>Author</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="tab-pane" id="coreModules">
            <div class="">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <table id="pageListDt" class="table table-striped table-bordered" cellspacing="0" width="100%">
                            <thead>
                                <tr><th>Name</th><th>Description</th>@*<th>Dependency</th>*@<th>Status</th></tr>
                            </thead>
                            <tbody>
                                @foreach (NccModule item in ViewBag.Modules)
                                {
                                    if (item.Path.Contains("\\Core\\") == true)
                                    {
                                        <tr>
                                            <td>
                                                <strong>@item.ModuleTitle </strong><br />
                                                @Html.Raw(Core.Admin.Lib.ModuleHelper.GetActionText(item))
                                            </td>
                                            <td>
                                                @item.Description
                                                <br />
                                                <i>
                                                    Version: <b>@item.Version</b> | By: <a href="@item.WebSite" target="_blank" style="font-size:12px;">@item.Author</a> | Category: @item.Category
                                                </i>
                                            </td>
                                            @*<td>
                                                @{ bool isFirst = true;}
                                                @foreach (var dep in item.Dependencies)
                                                {
                                                    if (isFirst == false)
                                                    {
                                                        <span>, </span>
                                                    }
                                                    isFirst = false;
                                                    <span>@dep.Name (@dep.VersionNumber)</span>
                                                }
                                            </td>*@
                                            <td>@item.ModuleStatus</td>
                                        </tr>
                                    }                                    
                                }
                            </tbody>
                        </table>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
 
@section Scripts{
    @*<link href="~/lib/DataTables/dataTables.bootstrap.css" rel="stylesheet" />
    <link href="~/lib/DataTables/jquery.dataTables.min.css" rel="stylesheet" />
    <script src="~/lib/DataTables/jquery.dataTables.min.js"></script>
    <script src="~/lib/DataTables/dataTables.bootstrap.min.js"></script>*@
    <script>
        $.ajax({
            method: 'GET',
            url: '/CmsModule/LoadNccStoreModules',
            data: { key: "" },
            success: function (rsp) {
                //console.log(rsp);
                if (rsp.isSuccess == true) {
                    rsp.data.forEach(function (item, index) {
                        //console.log(item);
                        $('#moduleGalleryOnline').append(
                            "<tr>" +
                            "   <td><b>" + item.moduleTitle + "</b><br/>" + item.description + "</td>" +
                            "   <td>" + item.version + "</td>" +
                            "   <td>" + item.category + "</td>" +
                            "   <td><a href='" + item.website + "' target='_blank'>" + item.author + "</td>" +
                            "   <td>" + "<input type='button' class='btn btn-success btn-xs btn-outline' value='Download' onclick='downloadModule(\"" + item.moduleName + "\",\"" + item.version+ "\")' />" + "</td>" +
                            "</tr >"
                        );
                    });
                }
                else {
                    NccGlobalAlert.ShowError(rsp.message);
                }
            },
            error: function (rsp) {
                //NccPageMask.HideLoadingMask();
                NccGlobalAlert.ShowError("Error occoured. Please try again after refresh the page.");
            }
        });

        function downloadModule(moduleId, version) {
            NccPageMask.ShowLoadingMask();
            $.ajax({
                method: 'POST',
                url: '/CmsModule/DownloadModule',
                data: { moduleName: moduleId, version: version},
                success: function (rsp) {
                    NccPageMask.HideLoadingMask();
                    console.log(rsp);
                    if(rsp.isSuccess==true)
                        NccGlobalAlert.ShowSuccess(rsp.message);
                    else
                        NccGlobalAlert.ShowError(rsp.message);
                },
                error: function (rsp) {
                    NccPageMask.HideLoadingMask();
                    NccGlobalAlert.ShowError("Error occoured. Please try again after refresh the page.");
                }
            });
            //NccGlobalAlert.ShowError(moduleId);
            NccPageMask.HideLoadingMask();

        }
    </script>
}
